import pandas as pd

def normalize_list(lst):
    amin, amax = min(lst), max(lst)
    for i, val in enumerate(lst):
        lst[i] = (val-amin) / (amax-amin)
    return lst

def main():
    # import variable data
    df_missions = pd.read_csv("variable_data/military_missions.csv")
    # filter for German missions
    df_missions = df_missions[df_missions.german_contribution == 1]
    # convert personnel into float variable
    for index, row in df_missions.iterrows():
        df_missions.loc[index, "personnel"] = float(row["personnel"])
    # create dataframe for dependent variable
    column_names = ["year", "missions", "personnel", "costs"]
    dv = pd.DataFrame(columns = column_names)
    # calculate socres for missions, personnel, and costs for every year
    for year in range(1990,2018):
        i = year - 1990
        missions = 0
        personnel = 0.0
        costs = 0.0
        duration = 0
        for index, row in df_missions.iterrows():
            duration = row["german_end"] - row["german_start"] + 1
            if (year >= row["german_start"]) & (year <= row["german_end"]):
                missions += 1
                personnel += row["personnel"] 
                costs += row["costs"] / duration
        # attach score to dataframe for confounding variable        
        dv.loc[i] = [str(year).split(".")[0], missions, personnel, costs]
    # normalize scores
    dv["missions_norm"] = normalize_list(dv["missions"].tolist())
    dv["personnel_norm"] = normalize_list(dv["personnel"].tolist())
    dv["costs_norm"] = normalize_list(dv["costs"].tolist())
    # calculate weights to adjust normalization
    temp_list = dv["personnel_norm"].tolist()
    temp_list = temp_list[25:]
    weight_personnel = (max(temp_list) - min(temp_list)) / max(temp_list)
    temp_list = dv["costs_norm"].tolist()
    temp_list = temp_list[25:]
    weight_costs = (max(temp_list) - min(temp_list)) / max(temp_list)
    temp_list = dv["missions_norm"].tolist()
    temp_list = temp_list[25:]
    weight_missions = (max(temp_list) - min(temp_list)) / max(temp_list)
    # calculate dv score
    dv["dv"] = 0.0
    for index, row in dv.iterrows():
        dv.loc[index, "dv"] = row["costs_norm"] * weight_costs + row["personnel_norm"] * weight_personnel + row["missions_norm"] * weight_missions
    dv["dv_norm"] = normalize_list(dv["dv"].tolist())
    # create lead dv
    dv_list = dv["dv_norm"].tolist()
    dv_list.pop(0)
    temp = [dv.loc[27, "dv_norm"]]
    dv_list.extend(temp)
    dv["dv_norm_lead"] = dv_list
    # export data
    df_export = dv[["year", "dv_norm", "dv_norm_lead"]]
    df_export.to_csv("dv.csv", index = False)

if __name__ == "__main__":
    main()